// 说明：JS时间Date格式化参数
// 参数：格式化字符串如：'yyyy-MM-dd hh:mm:ss'
// 结果：如2016-06-01 10:09:00
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    S: this.getMilliseconds(), // 毫秒
  };

  // 根据y的长度来截取年
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  }
  return fmt;
};

export function getWeekRange(date) {
  if (!date) return;

  var now = new Date(date);
  var nowDayOfWeek = now.getDay(); // 星期日—>六（0->6）
  var nowDay = now.getDate();
  var nowMonth = now.getMonth();
  var nowYear = now.getYear(); // 2019年是119
  nowYear += nowYear < 2000 ? 1900 : 0;
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1); // 这周的周五
  var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek)); // 7- 这周的周日  &&  用5- 得到这的周五
  return (
    weekStartDate.format("yyyy-MM-dd") +
    " 至 " +
    weekEndDate.format("yyyy-MM-dd")
  );
}

export function getWeekZh(value) {
  var dateArray = value.split("-");
  var zhWeek =
    "周" +
    "日一二三四五六".charAt(
      new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]).getDay()
    );
  return zhWeek;
}
